<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Transformations</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li><li class="navelem"><a class="el" href="../../d7/df9/tutorial_table_of_content_viz.html">OpenCV Viz</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Transformations </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../d8/df0/tutorial_widget_pose.html">Pose of a widget</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d2/d64/tutorial_creating_widgets.html">Creating Widgets</a></p>
<h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>How to use makeTransformToGlobal to compute pose</li>
<li>How to use makeCameraPose and Viz3d::setViewerPose</li>
<li>How to visualize camera position by axes and by viewing frustum</li>
</ul>
<h2>Code </h2>
<p>You can download the code from <a href="https://github.com/opencv/opencv_contrib/tree/master/modules/viz/samples/transformations.cpp">here</a>. </p><div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;<a class="code" href="../../d0/d7e/viz_8hpp.html">opencv2/viz.hpp</a>&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;fstream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help()</div><div class="line">{</div><div class="line">    cout</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;--------------------------------------------------------------------------&quot;</span>   &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;This program shows how to use makeTransformToGlobal() to compute required pose,&quot;</span></div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;how to use makeCameraPose and Viz3d::setViewerPose. You can observe the scene &quot;</span></div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;from camera point of view (C) or global point of view (G)&quot;</span>                    &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;Usage:&quot;</span>                                                                       &lt;&lt; endl</div><div class="line">    &lt;&lt; <span class="stringliteral">&quot;./transformations [ G | C ]&quot;</span>                                                 &lt;&lt; endl</div><div class="line">    &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> cvcloud_load()</div><div class="line">{</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> cloud(1, 1889, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga0610d99405b809062622588c25ed5c8f">CV_32FC3</a>);</div><div class="line">    ifstream ifs(<span class="stringliteral">&quot;bunny.ply&quot;</span>);</div><div class="line"></div><div class="line">    <span class="keywordtype">string</span> str;</div><div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0; i &lt; 12; ++i)</div><div class="line">        getline(ifs, str);</div><div class="line"></div><div class="line">    <a class="code" href="../../df/d6c/classcv_1_1Point3__.html">Point3f</a>* data = cloud.ptr&lt;<a class="code" href="../../df/d6c/classcv_1_1Point3__.html">cv::Point3f</a>&gt;();</div><div class="line">    <span class="keywordtype">float</span> dummy1, dummy2;</div><div class="line">    <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0; i &lt; 1889; ++i)</div><div class="line">        ifs &gt;&gt; data[i].x &gt;&gt; data[i].y &gt;&gt; data[i].z &gt;&gt; dummy1 &gt;&gt; dummy2;</div><div class="line"></div><div class="line">    cloud *= 5.0f;</div><div class="line">    <span class="keywordflow">return</span> cloud;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argn, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">    help();</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (argn &lt; 2)</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Missing arguments.&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">bool</span> camera_pov = (argv[1][0] == <span class="charliteral">&#39;C&#39;</span>);</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/d32/classcv_1_1viz_1_1Viz3d.html">viz::Viz3d</a> myWindow(<span class="stringliteral">&quot;Coordinate Frame&quot;</span>);</div><div class="line"></div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;Coordinate Widget&quot;</span>, <a class="code" href="../../df/d63/classcv_1_1viz_1_1WCoordinateSystem.html">viz::WCoordinateSystem</a>());</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/dcf/classcv_1_1Vec.html">Vec3f</a> cam_pos(3.0f,3.0f,3.0f), cam_focal_point(3.0f,3.0f,2.0f), cam_y_dir(-1.0f,0.0f,0.0f);</div><div class="line"></div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cam_pose = <a class="code" href="../../d1/d19/group__viz.html#gab04d43faa448940bb58754af6356955b">viz::makeCameraPose</a>(cam_pos, cam_focal_point, cam_y_dir);</div><div class="line"></div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> transform = <a class="code" href="../../d1/d19/group__viz.html#ga16ef52f42849f13a36b715d488ce5b9f">viz::makeTransformToGlobal</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,-1.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(-1.0f,0.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,-1.0f), cam_pos);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> bunny_cloud = cvcloud_load();</div><div class="line">    <a class="code" href="../../db/d82/classcv_1_1viz_1_1WCloud.html">viz::WCloud</a> cloud_widget(bunny_cloud, <a class="code" href="../../d4/dba/classcv_1_1viz_1_1Color.html#a7b6c40bb6c8a6aec8f28a358913db3e2">viz::Color::green</a>());</div><div class="line"></div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cloud_pose = <a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a>().<a class="code" href="../../dd/d99/classcv_1_1Affine3.html#a11d9cdc588a9c08c0c1c205a872663fd">translate</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,3.0f));</div><div class="line">    <a class="code" href="../../dd/d99/classcv_1_1Affine3.html">Affine3f</a> cloud_pose_global = transform * cloud_pose;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (!camera_pov)</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d6/dcc/classcv_1_1viz_1_1WCameraPosition.html">viz::WCameraPosition</a> cpw(0.5); <span class="comment">// Coordinate axes</span></div><div class="line">        <a class="code" href="../../d6/dcc/classcv_1_1viz_1_1WCameraPosition.html">viz::WCameraPosition</a> cpw_frustum(<a class="code" href="../../dc/d84/group__core__basic.html#ga392bb4f8a6b9e0dde07f31dc28e73319">Vec2f</a>(0.889484, 0.523599)); <span class="comment">// Camera frustum</span></div><div class="line">        myWindow.showWidget(<span class="stringliteral">&quot;CPW&quot;</span>, cpw, cam_pose);</div><div class="line">        myWindow.showWidget(<span class="stringliteral">&quot;CPW_FRUSTUM&quot;</span>, cpw_frustum, cam_pose);</div><div class="line">    }</div><div class="line"></div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;bunny&quot;</span>, cloud_widget, cloud_pose_global);</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> (camera_pov)</div><div class="line">        myWindow.setViewerPose(cam_pose);</div><div class="line"></div><div class="line">    myWindow.spin();</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --><h2>Explanation </h2>
<p>Here is the general structure of the program:</p>
<ul>
<li>Create a visualization window. <div class="fragment"><div class="line">viz::Viz3d myWindow(<span class="stringliteral">&quot;Transformations&quot;</span>);</div></div><!-- fragment --></li>
<li>Get camera pose from camera position, camera focal point and y direction. <div class="fragment"><div class="line"><a class="code" href="../../dc/d84/group__core__basic.html#ga3d79ceeb4419bccd0308dfdf1cd31435">Point3f</a> cam_pos(3.0f,3.0f,3.0f), cam_focal_point(3.0f,3.0f,2.0f), cam_y_dir(-1.0f,0.0f,0.0f);</div><div class="line"></div><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> cam_pose = <a class="code" href="../../d1/d19/group__viz.html#gab04d43faa448940bb58754af6356955b">viz::makeCameraPose</a>(cam_pos, cam_focal_point, cam_y_dir);</div></div><!-- fragment --></li>
<li>Obtain transform matrix knowing the axes of camera coordinate system. <div class="fragment"><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> transform = <a class="code" href="../../d1/d19/group__viz.html#ga16ef52f42849f13a36b715d488ce5b9f">viz::makeTransformToGlobal</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,-1.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(-1.0f,0.0f,0.0f), <a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,-1.0f), cam_pos);</div></div><!-- fragment --></li>
<li>Create a cloud widget from bunny.ply file <div class="fragment"><div class="line">Mat bunny_cloud = cvcloud_load();</div><div class="line">viz::WCloud cloud_widget(bunny_cloud, viz::Color::green());</div></div><!-- fragment --></li>
<li>Given the pose in camera coordinate system, estimate the global pose. <div class="fragment"><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> cloud_pose = <a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a>().<a class="code" href="../../dd/d99/classcv_1_1Affine3.html#a11d9cdc588a9c08c0c1c205a872663fd">translate</a>(<a class="code" href="../../dc/d84/group__core__basic.html#gab9e0ab642a3e01742916763173b72232">Vec3f</a>(0.0f,0.0f,3.0f));</div><div class="line"><a class="code" href="../../d0/de1/group__core.html#gae10a81cba3f3232648be48e28759a7bc">Affine3f</a> cloud_pose_global = transform * cloud_pose;</div></div><!-- fragment --></li>
<li>If the view point is set to be global, visualize camera coordinate frame and viewing frustum. <div class="fragment"><div class="line"><span class="keywordflow">if</span> (!camera_pov)</div><div class="line">{</div><div class="line">    viz::WCameraPosition cpw(0.5); <span class="comment">// Coordinate axes</span></div><div class="line">    viz::WCameraPosition cpw_frustum(<a class="code" href="../../dc/d84/group__core__basic.html#ga392bb4f8a6b9e0dde07f31dc28e73319">Vec2f</a>(0.889484, 0.523599)); <span class="comment">// Camera frustum</span></div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;CPW&quot;</span>, cpw, cam_pose);</div><div class="line">    myWindow.showWidget(<span class="stringliteral">&quot;CPW_FRUSTUM&quot;</span>, cpw_frustum, cam_pose);</div><div class="line">}</div></div><!-- fragment --></li>
<li>Visualize the cloud widget with the estimated global pose <div class="fragment"><div class="line">myWindow.showWidget(<span class="stringliteral">&quot;bunny&quot;</span>, cloud_widget, cloud_pose_global);</div></div><!-- fragment --></li>
<li>If the view point is set to be camera's, set viewer pose to <b>cam_pose</b>. <div class="fragment"><div class="line"><span class="keywordflow">if</span> (camera_pov)</div><div class="line">    myWindow.setViewerPose(cam_pose);</div></div><!-- fragment --></li>
</ul>
<h2>Results </h2>
<ol type="1">
<li>Here is the result from the camera point of view.</li>
</ol>
<div class="image">
<img src="../../camera_view_point.png" alt="camera_view_point.png"/>
</div>
<ol type="1">
<li>Here is the result from global point of view.</li>
</ol>
<div class="image">
<img src="../../global_view_point.png" alt="global_view_point.png"/>
</div>
 </div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:39 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
